import { defineStore } from 'pinia'
import axios from 'axios'

interface Homework {
  id: string
  courseId: string
  title: string
  description: string
  deadline: string
  maxScore: number
  attachmentUrl?: string
}

interface HomeworkSubmission {
  id: string
  homeworkId: string
  studentId: string
  submissionUrl: string
  submissionText?: string
  submissionDate: string
  grade?: number
  feedback?: string
  status: 'PENDING' | 'GRADED' | 'LATE'
}

export const useHomeworkStore = defineStore('homework', {
  state: () => ({
    homeworks: [] as Homework[],
    submissions: [] as HomeworkSubmission[],
    currentHomework: null as Homework | null,
    loading: false,
    error: null as string | null
  }),

  actions: {
    async fetchCourseHomeworks(courseId: string) {
      try {
        this.loading = true
        const response = await axios.get(`/api/courses/${courseId}/homeworks`)
        this.homeworks = response.data
        return response.data
      } catch (error) {
        this.error = '获取作业列表失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    async getHomeworkDetail(homeworkId: string) {
      try {
        this.loading = true
        const response = await axios.get(`/api/homeworks/${homeworkId}`)
        this.currentHomework = response.data
        return response.data
      } catch (error) {
        this.error = '获取作业详情失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    async submitHomework(homeworkId: string, submission: FormData) {
      try {
        this.loading = true
        const response = await axios.post(`/api/homeworks/${homeworkId}/submit`, submission, {
          headers: { 'Content-Type': 'multipart/form-data' }
        })
        return response.data
      } catch (error) {
        this.error = '提交作业失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    async fetchStudentSubmissions(courseId: string) {
      try {
        this.loading = true
        const response = await axios.get(`/api/courses/${courseId}/submissions`)
        this.submissions = response.data
        return response.data
      } catch (error) {
        this.error = '获取学生作业提交列表失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    async gradeHomeworkSubmission(submissionId: string, gradeData: { grade: number; feedback?: string }) {
      try {
        this.loading = true
        const response = await axios.post(`/api/submissions/${submissionId}/grade`, gradeData)
        
        // 更新本地提交状态
        const submissionIndex = this.submissions.findIndex(submission => submission.id === submissionId)
        if (submissionIndex !== -1) {
          this.submissions[submissionIndex] = {
            ...this.submissions[submissionIndex],
            ...gradeData,
            status: 'GRADED'
          }
        }

        return response.data
      } catch (error) {
        this.error = '批改作业失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    async getPendingHomeworks() {
      try {
        const response = await axios.get('/api/homeworks/pending')
        return response.data
      } catch (error) {
        this.error = '获取待批改作业失败'
        throw error
      }
    }
  }
})
